package bookManageSystem.dao;

import bookManageSystem.po.Employee;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDAO {

    private static final String USER = "root";
    private static final String PASS = "123456";
    private static final String DB_URL = "jdbc:mysql://localhost:3306/huaide?useUnicode=true&characterEncoding=UTF-8";

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(DB_URL, USER, PASS);
    }

    public boolean addEmployee(Employee employee) {
        String sql = "INSERT INTO employees (userId, username, password, phone, balance) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, employee.getUserId());
            pstmt.setString(2, employee.getUsername());
            pstmt.setString(3, employee.getPassword());
            pstmt.setString(4, employee.getPhone());
            pstmt.setDouble(5, employee.getBalance());

            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected == 1;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<Employee> getAllEmployees() {
        List<Employee> employees = new ArrayList<>();
        String sql = "SELECT * FROM employees";
        try (Connection conn = getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Employee employee = new Employee();
                employee.setUserId(rs.getInt("userId"));
                employee.setUsername(rs.getString("username"));
                employee.setPassword(rs.getString("password"));
                employee.setPhone(rs.getString("phone"));
                employee.setBalance(rs.getDouble("balance"));
                employees.add(employee);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return employees;
    }

    public Employee getEmployeeById(int id) {
        Employee employee = null;
        String sql = "SELECT * FROM employees WHERE userId = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    employee = new Employee();
                    employee.setUserId(rs.getInt("userId"));
                    employee.setUsername(rs.getString("username"));
                    employee.setPassword(rs.getString("password"));
                    employee.setPhone(rs.getString("phone"));
                    employee.setBalance(rs.getDouble("balance"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return employee;
    }

    public Boolean updateEmployee(Employee selectedEmployee) {
        String sql = "UPDATE employees SET username = ?, password = ?, phone = ?, balance = ? WHERE userId = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, selectedEmployee.getUsername());
            stmt.setString(2, selectedEmployee.getPassword());
            stmt.setString(3, selectedEmployee.getPhone());
            stmt.setDouble(4, selectedEmployee.getBalance());
            stmt.setInt(5, selectedEmployee.getUserId());

            int rowsAffected = stmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public Boolean deleteEmployee(int id) {
        String sql = "DELETE FROM employees WHERE userId = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            int rowsAffected = stmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public Employee getEmployeeByUsername(String username) {
        Employee employee = null;
        String sql = "SELECT * FROM employees WHERE username = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, username);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    employee = new Employee();
                    employee.setUserId(rs.getInt("userId"));
                    employee.setUsername(rs.getString("username"));
                    employee.setPassword(rs.getString("password"));
                    employee.setPhone(rs.getString("phone"));
                    employee.setBalance(rs.getDouble("balance"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return employee;
    }
    public Boolean updateEmployeeByUserId(int userId) {
        String sql = "UPDATE employees SET balance = balance - 6 WHERE userId = ?";
        try (Connection conn = getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, userId);
            int rowsAffected = stmt.executeUpdate();
            return rowsAffected > 0; // 如果影响的行数大于0，说明更新成功
        } catch (SQLException e) {
            e.printStackTrace();
            return false; // 如果发生异常，返回false
        }
    }
}